package interview;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description interview
 * @since JDK1.8
 */
public class IntNPow {
    public double myPow(double x, int n) {
        if (x == 0) {
            return 0;
        } else {
            return pow(n, x, 1);
        }
    }

    private double pow(int n, double m, int t) {
        if (n == 0) {
            return pow(n, m, t + 1);
        }

        if ((n & 1) == 0) {
            return m * pow((n >> t), m, t + 1);
        } else {
            double temp = 1;
            double p = m * m;
            for (int i = 1; i < t; i++) {
                temp *= p;
            }

            return temp * pow((n >> t), m, t + 1);
        }
    }
}
